home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d20
/
doorskl3.arc
/
SELECT1.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-01-15
|
12KB
|
413 lines
/*
* More fancy input routines. select_one is ANSI or ASCII. top_menu is
* ANSI only.
*
*/
#include "doorskel.h"
#include "keys.h"
int _fastcall select_one (char **selections,int numselecs,char *prompt,
unsigned int flags) {
int x = 0,y,oldx,oldlen,longest = 0,perline = 1,timeout = 0;
unsigned int temp;
time_t startt;
/* 'rolling' input, ASCII or ANSI. returns -1 (aborted) or # of option
selected.
bitmapped flags:
1: Show initial list of options
2: Show initial list of first letters
4: No roll, just regular hot input
*/
if(flags & 1) goto Options;
RePrompt:
if(flags & 2) {
int x;
printm("\r\n[");
for(x = 0;x < numselecs;x++) {
if(*selections[x] != '*') printfm("%c",*selections[x]);
}
printm("?] ");
}
if(prompt && *prompt) printm(prompt);
for(;;) {
getxbbstime();
if(!(flags & 4)) {
oldx = x;
oldlen = printm(selections[x]);
}
Another:
startt = getxbbstime() + 241L;
do {
if(startt < getxbbstime()) return -1;
temp = (int)inkey();
} while(!temp);
if(temp == '\r') {
if(flags & 4) {
return 0; /* default to initial option */
}
else {
return x; /* return current option */
}
}
if(!(flags & 4)) {
switch(temp) {
case PGUP:
case UP:
case RIGHT:
case '9':
case '8':
case '6':
case ' ':
x++;
if(x >= numselecs) x = 0;
goto BotLoop;
case DN:
case LEFT:
case PGDN:
case '3':
case '4':
case '2':
x--;
if(x < 0) x = numselecs - 1;
goto BotLoop;
case END:
case '1':
x = numselecs - 1;
goto BotLoop;
case HOME:
case '7':
x = 0;
goto BotLoop;
default: break;
}
}
switch(temp) {
case '?':
Options:
printm("\r\nAvailable options:\r\n");
{
int x;
if(!longest) {
for(x = 0;x < numselecs;x++) {
y = strlen(selections[x]);
longest = max(longest,y);
}
perline = width / (longest + 2);
}
for(x = 0;x < numselecs;x++) {
if(longest <= ((int)width / perline) - 1) {
if((x + 1) % perline) {
printm(" ");
printm(selections[x]);
if(x < numselecs - 1) {
for(y = 0;y < (((int)width / perline) - 1) - strlen(selections[x]);y++) {
printm(" ");
}
}
else printm("\r\n");
}
else {
printfm(" %s\r\n",selections[x]);
}
}
else printfm(" %s\r\n",selections[x]);
}
}
goto RePrompt;
case ESC:
case CTRL_K:
return -1;
default:
temp = toupper(temp);
for(y = 0;y < numselecs;y++) {
if(toupper(*selections[y]) == (int)temp) {
if(!(flags & 4)) {
if(y != oldx) {
backup(oldlen);
printm(selections[y]);
}
}
else printfm("%c",*selections[y]);
return y;
}
}
if(!(flags & 4)) {
if(graphics) {
printm("\r\x1b[KKeypad/cursor toggles, [Enter] selects, ? lists, [ESC/CTRL-K] aborts");
fossil(PURGEIN,0);
DosSleep(2250L);
printm("\r\x1b[K");
}
else {
printm("\r\nKeypad/cursor toggles, [Enter] selects, ? lists, [ESC/CTRL-K] aborts\r\n");
DosSleep(100L);
fossil(PURGEIN,0);
}
}
else {
printm("\r\07");
printg("\x1b[K");
fossil(PURGEIN,0);
}
goto RePrompt;
}
BotLoop:
if(!(flags & 4)) {
backup(oldlen);
}
else if(isprint(temp)) {
backup(1);
}
}
}
int _fastcall is_one (int key,char **selections,int numselecs) {
/* checks a key to see if it's one of the selections -- -1 = no match */
register int x;
if(key && key != -1) {
for(x = 0;x < numselecs;x++) {
if(key == (int)*selections[x]) return x;
}
}
return -1;
}
int _fastcall top_menu (char **selections,int numselects) {
int x,ts = 0,first = 0,last,cur = 0,len,redisplay = 1,temp,lastcur = 32767;
char *more = "<More>";
time_t startt;
/* top bar menu, ANSI only. returns -1 (aborted) or # of option chosen */
fossil(PURGEIN,0);
ReStart:
printm("\x1b[s\x1b[0m");
for(;;) {
if(redisplay) {
printm("\x1b[1;1H\x1b[7m\x1b[K ");
len = 1;
first = ts;
for(x = first; ;x++) {
len += printm(selections[x]);
len += printm(" ");
if(x == numselects - 1 ||
len > width - (strlen(more) +
strlen(selections[x + 1]) + 2)) {
last = x;
break;
}
}
last = x;
if(cur <= first) cur = first;
else cur = last;
if(first > 0 || last < numselects - 1) len += printm(more);
while(len++ < width) printm(" ");
}
if(cur != lastcur || redisplay) {
len = 1;
for(x = first;x <= last;x++) {
if(x == cur) {
printfm("\x1b[1;%dH\x1b[0m%s\x1b[7m",len + 1,selections[x]);
if(redisplay || lastcur == 32767) break;
}
if(x == lastcur) {
printfm("\x1b[1;%dH%s",len + 1,selections[x]);
}
len += strlen(selections[x]) + 2;
}
redisplay = 0;
lastcur = cur;
}
ReInput:
startt = getxbbstime() + 241L;
do {
if(startt < getxbbstime()) return -1;
temp = (int)inkey();
} while(!temp);
switch(temp) {
case ESC:
case CTRL_K:
printm("\x1b[0m\x1b[u");
return -1;
case '\r':
printm("\x1b[0m\x1b[u");
return cur;
case '4':
case LEFT:
if(!cur) {
cur = numselects - 1;
len = 1;
for(x = numselects - 1; ;x--) {
len += strlen(selections[x]) + 2;
if(!x ||
len > width - (strlen(more) +
strlen(selections[x - 1]) + 2)) {
ts = x;
break;
}
}
if(ts) redisplay = 1;
break;
}
else cur--;
if(cur >= first && cur <= last) break; /* else fallthrough to UP handler */
case '8':
case '9':
case PGUP:
case UP:
if(first > 0 || last < numselects - 1) {
if(ts) {
len = 1;
for(x = ts - 1; ;x--) {
len += strlen(selections[x]) + 2;
if(!x ||
len > width - (strlen(more) +
strlen(selections[x - 1]) + 2)) {
cur = ts - 1;
ts = x;
break;
}
}
redisplay = 1;
break;
} /* else fallthrough to END handler */
}
else {
printm("\07");
break;
}
case '1':
case END:
if(first > 0 || last < numselects - 1) {
cur = numselects - 1;
len = 1;
for(x = numselects - 1; ;x--) {
len += strlen(selections[x]) + 2;
if(!x ||
len > width - (strlen(more) +
strlen(selections[x - 1]) + 2)) {
ts = x;
break;
}
}
redisplay = 1;
}
else printm("\07");
break;
case ' ':
case '6':
case RIGHT:
cur++;
if(cur > numselects - 1) {
cur = 0;
ts = 0;
if(first > 0) redisplay = 1;
break;
}
if(cur <= last) break; /* else fallthrough to DN handler */
case '2':
case '3':
case PGDN:
case DN:
if(first > 0 || last < numselects - 1) {
ts = last + 1;
if(ts > numselects - 1) ts = 0;
redisplay = 1;
}
else printm("\07");
break;
case '7':
case HOME:
cur = 0;
ts = 0;
redisplay = 1;
break;
default:
temp = toupper(temp);
for(x = first;x < last + 1;x++) { /* first try what's showing */
if(toupper(*selections[x]) == temp) {
printm("\x1b[0m\x1b[u");
return x;
}
}
for(x = 0;x < numselects;x++) { /* then try everything */
if(toupper(*selections[x]) == temp) {
printm("\x1b[0m\x1b[u");
return x;
}
}
printm("\07");
goto ReInput;
}
}
printm("\x1b[0m\x1b[u");
return -1;
}
void _fastcall backup (int num) {
/* prints num number of backspaces to overwrite a string */
while(num--) printm(BACKSPACE);
}